package com.ruoyi.project.blog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.bean.ResponseResult;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.blog.domain.BCategory;
import com.ruoyi.project.blog.domain.vo.SystemCategoryListVO;
import com.ruoyi.project.blog.domain.vo.SystemTagListVo;
import com.ruoyi.project.blog.service.IBCategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 博客分类Controller
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@RestController
@RequestMapping("/articles/category")
public class BCategoryController extends BaseController {
    @Autowired
    private IBCategoryService bCategoryService;

    @Anonymous
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ApiOperation(value = "分类列表", httpMethod = "GET", response = ResponseResult.class, notes = "分类列表")
    public ResponseResult selectCategoryList(Long userId) {
        return bCategoryService.selectCategoryList(userId);
    }

    /**
     * 查询博客分类列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BCategory bCategory) {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (!admin) {
            bCategory.setUserId(SecurityUtils.getUserId().toString());
        }
        startPage();
        List<SystemCategoryListVO> list = bCategoryService.selectBCategoryList(bCategory);
        return getDataTable(list);
    }

    /**
     * 导出博客分类列表
     */
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BCategory bCategory) {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (!admin) {
            bCategory.setUserId(SecurityUtils.getUserId().toString());
        }
        List<SystemCategoryListVO> list = bCategoryService.selectBCategoryList(bCategory);
        ExcelUtil<SystemCategoryListVO> util = new ExcelUtil<SystemCategoryListVO>(SystemCategoryListVO.class);
        util.exportExcel(response, list, "博客分类数据");
    }

    /**
     * 获取博客分类详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bCategoryService.selectBCategoryById(id));
    }

    /**
     * 新增博客分类
     */
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BCategory bCategory) {
        bCategory.setUserId(SecurityUtils.getUserId().toString());
        return bCategoryService.insertBCategory(bCategory);
    }

    /**
     * 修改博客分类
     */
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BCategory bCategory) {
        return bCategoryService.updateBCategory(bCategory);
    }

    /**
     * 删除博客分类
     */
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bCategoryService.deleteBCategoryByIds(ids));
    }

    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(String name) {
        List<SystemCategoryListVO> list = bCategoryService.listTags(name);
        return AjaxResult.success(list);
    }
}
